<template>
  <div class="app-container">
    <!-- 查询 -->
    <el-form :inline="true" :model="list" class="demo-form-inline">
      <el-form-item label="医院名称">
        <el-input v-model="searchObj.hosname" placeholder="请输入医院名称" ></el-input>
      </el-form-item>
      <el-form-item label="医院编码">
        <el-input v-model="searchObj.hoscode" placeholder="请输入医院编码"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="getList()" icon="el-icon-search">查询</el-button>
        <el-button type="primary" @click="resetData()">重置</el-button>
      </el-form-item>
    </el-form>
      
    <!-- 批量删除按钮 -->
    <div>
      <el-button type="danger" size="mini" @click="removeRows()">批量删除</el-button>
    </div>
      
    <!-- banner列表 -->
    <el-table :data="list" stripe style="width: 100%" @selection-change="handleSelectionChange"> 
      
      <!-- 复选框 -->
      <el-table-column type="selection" width="55"/>

      <el-table-column type="index" width="50" label="序号" />
      <el-table-column prop="hosname" label="医院名称" />
      <el-table-column prop="hoscode" label="医院编号" />
      <el-table-column prop="apiUrl" label="api基础路径" width="200" />
      <el-table-column prop="contactsName" label="联系人姓名" />
      <el-table-column prop="contactsPhone" label="联系人手机" />
      <el-table-column label="状态" width="80">
        <template slot-scope="scope">
          {{ scope.row.status === 1 ? "可用" : "不可用" }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="280" align="center">
        <template slot-scope="scope">
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeDataById(scope.row.id)"> </el-button>
          <router-link :to="'/hospSet/edit/'+scope.row.id">
             <el-button type="primary" size="mini" icon="el-icon-edit"></el-button>
          </router-link>
          <el-button v-if="scope.row.status==1" type="danger" size="mini" icon="el-icon-lock" @click="lock(scope.row.id,0)">锁定医院</el-button>
          <el-button v-if="scope.row.status==0" type="success" size="mini"  icon="el-icon-unlock" @click="lock(scope.row.id,1)">取消锁定</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="block">
      <el-pagination
        :current-page="current"
        :page-size="limit"
        :total="total"
        style="padding: 30px 0; text-align: center"
        layout="total, prev, pager, next, jumper"
        @current-change="getList"
      />
    </div>
  </div>
</template>

<script>
/* 引入接口定义文件 */
import hospset from "@/api/hospset";
export default {
  data() {
    return {
      current: 1,
      limit: 5,
      searchObj: {}, // 筛选参数
      list: [], // 数据集合
      total: 0, // 总记录数
      multipleSelection: [], // 选中记录集合
    };
  },
  created() {
    // 调用method定义的方法 获取要初始化的数据
    this.getList(this.current);
  },
  methods: {
    // 定义方法 用来请求接口 page为当前页的参数
    getList(page = 1) {
      this.current = page;
      hospset
        .getHospSetList(this.current, this.limit, this.searchObj)
        .then((Response) => {
          this.list = Response.data.records;
          this.total = Response.data.total;
        })
        .catch((error) => {
          alert(error);
        });
    },
    // 重置按钮
    resetData() {
      this.searchObj = {};
      this.getList();
    },
    // 删除医院设置方法
    removeDataById(id){
      this.$confirm('此操作将永久删除医院设置信息, 是否继续?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
      }).then(()=>{
          hospset.delHospSet(id)
          .then(response => {
            //提示
            this.$message({
               type: 'success',
               message: '删除成功!'
            })
            //刷新页面
            this.getList(1);
          })
      })
    },
    // 单选框被改变事件 由table监听
    handleSelectionChange(selection){
      // 将选择的对象设置到multipleselection中
      this.multipleSelection = selection;
    },
    // 批量删除
    removeRows(){
      if(this.multipleSelection==null|| this.multipleSelection==""){
          this.$message({
            type: 'warning',
            message: '未选中有效数据'
          })
      }else{
        this.$confirm('此操作将永久删除医院设置信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
        }).then(()=>{
          var ids = [];
          for(var i = 0;i < this.multipleSelection.length;i++){
            // 获取id值
            var id = this.multipleSelection[i].id;
            // 将id值插入数组
            ids.push(id);
          }
          // 调用方法实现批量删除
          hospset.delHospSetBatch(ids).then(response=>{
            //提示
            this.$message({
              type: 'success',
              message: `批量删除成功! 共删除${ids.length}条`
            })
            //刷新页面
            this.getList(1);
          })
        })
      }
   },
   lock(id,status){
     hospset.lock(id,status).then(response =>{
      this.$message({
        type: 'success',
        message: '成功'
      })
      this.getList(this.current);
     })
   }
  }
};
</script>
<style>
@import url("//unpkg.com/element-ui@2.15.3/lib/theme-chalk/index.css");
</style>